<?php
declare (strict_types = 1);

namespace app\listener\notify;

use app\model\CouponOrder;
use app\model\SmsLog;

class CouponOrderSuccessSmsNotice
{
    /**
     * 事件监听处理
     *
     * @return mixed
     */
    public function handle($event)
    {
        $couponOrder = $event->couponOrder;

        if ($couponOrder->serv_type != PRODUCT_COUPON_SERV ) {
            return true;
        }

        if ($couponOrder->type != CouponOrder::TYPE_COUPON_SELF_EMPLOYED) {
            return true;
        }


        //发送短信

        if (isLocal()) {
            return true;
        }

        if (SmsLog::query()->where('record_id', $couponOrder->id)->find()) {
            return false;
        }

        //发送短信
        SmsLog::findOrCreate([
            'type' => SmsLog::COUPON_ORDER_ORDER_SUCCESS_TYPE,
            'params' => json_encode([
                'template' =>  SmsLog::COUPON_ORDER_ORDER_SUCCESS_TYPE_ALI_SMS_CODE,
                'data' => [
                    'content' => $couponOrder->product_name,
                    'number' => $couponOrder->account,
                ],
            ], JSON_UNESCAPED_UNICODE),
            'mobile' => json_encode([$couponOrder->account]),
            'record_id' => $couponOrder->id,
            'result' => '',
        ]);

    }
}
